<template>


  <view>

    <u-navbar title="首页" :is-back="false"></u-navbar>
<!--    <u-swiper-->
<!--        :list="list"-->
<!--        :autoplay="false"-->
<!--    ></u-swiper>-->
    <uni-notice-bar show-icon scrollable text="您有新的信息，请及时查看" v-if="msgNum!=0"/>
    <uni-section title="通知公告" type="line">
      <notice-view :notices="notices" ></notice-view>
      <o-empty v-if="notices.length==0"></o-empty>
      <view style="margin: auto;margin-top: 30rpx;padding-bottom:20rpx ">
        <view @click="checkMoreNotice" style="text-align: center;display: block;color: #b2b2b2">查看更多<uni-icons color="#b2b2b2" type="right"/></view></view>
    </uni-section>

    <uni-section title="待维修单" type="line">
      <repair-card :repair-data="repairOrders" :actions="cardActions"
      @startRepair="startRepair" @endRepair="endRepair" @contact="contact"
                   @undo="undo"
      ></repair-card>
    </uni-section>
  </view>
</template>

<script>
import NoticeView from "../../components/NoticeView";
import {NoticeService} from "../../normalUser/public/Notice/noticeService";
import {RepairOrderService} from "../../normalUser/public/repairApplication/repairOrderService";
import RepairCard from "../../components/RepairCard";

export default {
  components: {RepairCard, NoticeView},
  props:{
    msgNum:{
      default:0,
    }
  },
  data() {
    return {
      list: [],
      cardActions:[
        {
          title:"开始维修",
          icon:"gear",
          method:"startRepair",
          showStandard:"currentState",
          value:[1]
        },
        {
          title:"完成维修",
          icon:"checkmarkempty",
          method:"endRepair",
          showStandard:"currentState",
          value:[2]
        },
        {
          title:"退单",
          icon:"undo-filled",
          method:"undo",
          showStandard:"currentState",
          value:[1,2]
        },
        {
          title:"联系用户",
          icon:"phone",
          method:"contact",
          showStandard:"currentState",
          value:[1,2,3,4]
        },
      ],
      notices:[],
      repairOrders:[],
      refreshNotice:  false,
    }
  },
  created(){
    this.init();
  },
  watch:{
    // msgNum: {
    //   handler(newValue, oldValue) {
    //     console.log("new",newValue)
    //   },
    //   deep: true
    // },
    refreshNotice: {
      handler(newValue, oldValue) {
        if(newValue){
          this.refreshNotice=false;
          this.findNoticesNotContent();
        }
      },
      deep: true
    },
  },
  methods: {
    onClick(e) {
      console.log(e)
    },
    actionsClick(text) {
      uni.showToast({
        title: text,
        icon: 'none'
      })
    },
    contact(item){
      uni.makePhoneCall({
        phoneNumber: item.repairApplicantPhone //仅为示例
      });
    },
    init(){
      this.findNoticesNotContent()
      this.findRepairOrder()
    },
    //查询我发布的正在进行的维修单
    findRepairOrder(){
      let queryParams={
        currentPage:1,
        pageSize:100,
        filters:{
          repairmanId:uni.getStorageSync('userInfo').eid,
          currentState:"1$|$2"
        },
        orderBys:[
          {
            property: "createDatetime",
            order: "desc"
          }
        ]
      }
      RepairOrderService.findRepairOrders(queryParams).then(res=>{
        this.repairOrders=res.data.datas;
      })
    },
    findNoticesNotContent(){
      let queryParams={
        currentPage:1,
        pageSize:2,
        filters:{
          userId:uni.getStorageSync('userInfo').eid
        },
        orderBys:[
          {
            property: "createDatetime",
            order: "desc"
          }
        ]
      }
      NoticeService.findNoticesNotContent(queryParams).then(res=>{
        // console.log(res)
        this.notices=res.data.datas
      })
    },
    goRepair(){
      uni.navigateTo({
        //保留当前页面，跳转到应用内的某个页面
        url: '/pages/normalUser/repairApplication/index'
      })
    },
    undo(item){
      let _this=this
      uni.showModal({
        title: '退单确认',
        content: '',
        editable:true,
        placeholderText:"请输入退单原因",
        success: function (res) {
          if (res.confirm) {
            RepairOrderService.chargeback(item.eid,res.content).then(res=>{
              if(res.data.code==0){
                uni.showToast({
                  title:"退单成功"
                })
                _this.findRepairOrder();
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消');
          }
        }
      });
    },
    checkMoreNotice(){
      uni.navigateTo({
        //保留当前页面，跳转到应用内的某个页面
        url: '/pages/normalUser/public/Notice/index'
      })
    },
    startRepair(item){
      RepairOrderService.startRepair(item.eid).then(res=>{
        if(res.data.code==0){
          uni.showToast({
            title: "操作成功",
            icon: 'none'
          })
          this.findRepairOrder();
        }else{
          uni.showToast({
            title: "操作失败",
            icon: 'none'
          })
        }
      }).catch(err=>{
        uni.showToast({
          title: err.message,
          icon: 'none'
        })
      })
    },
    endRepair(item){
      RepairOrderService.endRepair(item.eid).then(res=>{
        if(res.data.code==0){
          uni.showToast({
            title: "操作成功",
            icon: 'none'
          })
          this.findRepairOrder()
        }else{
          uni.showToast({
            title: "操作失败",
            icon: 'none'
          })
        }
      }).catch(err=>{
        uni.showToast({
          title: err.message,
          icon: 'none'
        })
      })
    }
  }
}
</script>

<style>
.homepageIcon {
  max-width: 60%;
  margin: auto;
  display: block
}

.title {
  text-align: center;
  display: block;
}

.normalServeItem {
  width: 25%;
  height: 120rpx;
  display: inline-block;
}
</style>